# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/10/22 16:44
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 主要接口文件
from pathlib import Path
from fastapi import APIRouter, UploadFile, Form
from pydantic import BaseModel
from utils.file.file_manage import FileManage
from utils.response import SuccessResponse

app = APIRouter()

###########################################################
#    文件上传管理
###########################################################
@app.post("/upload/file", summary="上传文件到本地")
async def upload_image_to_local(file: UploadFile, path: str = Form(...)):
    manage = FileManage(file, path)
    path = await manage.async_save_local()
    return SuccessResponse(path)


class RemovePath(BaseModel):
    path: str

@app.delete("/upload/file", summary="删除文件")
async def delete_upload_file(data: RemovePath):
    path = Path(data.path)
    if path.exists() and path.is_file():
        path.unlink()
    return SuccessResponse("删除成功")